package com.zdsoft.blp.zongbuju.shouye.taoshihui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.isLoginSuccess.IsLoginSuccess;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by pc on 2016/9/2.
 */
public class OrderActivity extends BaseActivity implements NetCallback{
    private FanHuiView fh;
    private TextView tv_commit,tv_goodsName,tv_shuliang,tv_discountPrice,tv_zongjia;
    private EditText et_number;
    private ImageView iv_jian, iv_jia;
    private double price ,zPrice;
    @Override
    protected void init() {
        setContentView(R.layout.tsh_dingdxq);

        fh = find(R.id.fh);
        iv_jian = find(R.id.iv_jian);
        iv_jia = find(R.id.iv_jia);
        tv_commit = find(R.id.tv_commit);
        et_number = find(R.id.et_number);
        tv_goodsName = find(R.id.tv_goodsName);
        tv_shuliang = find(R.id.tv_shuliang);
        tv_discountPrice = find(R.id.tv_discountPrice);
        tv_zongjia = find(R.id.tv_zongjia);
        et_number.setSelection(et_number.length());
        fh.getTv_title().setText("订单详情");
        tv_goodsName.setText(getIntent().getStringExtra("goodsName"));
        tv_shuliang.setText(getIntent().getStringExtra("goodsNum"));
        tv_discountPrice.setText(getIntent().getStringExtra("discountPrice")+"元");
        price = Double.valueOf(getIntent().getStringExtra("discountPrice"));
        zPrice =price;
        tv_zongjia.setText(price+"元");
        tv_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(IsLoginSuccess.isloginSuccess(OrderActivity.this,true)&&!"".equals(et_number.getText().toString())){
                    commitOrder();
                }
            }
        });
        iv_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.valueOf(et_number.getText().toString()) > 1) {
                    et_number.setText((Integer.valueOf(et_number.getText().toString()) - 1) + "");
                    et_number.setSelection(et_number.length());
                    zPrice = Integer.valueOf(et_number.getText().toString())*price;
                    tv_zongjia.setText(zPrice+"元");
                }
            }
        });

        iv_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("".equals(et_number.getText().toString())){
                    et_number.setText(1+"");
                    et_number.setSelection(et_number.length());

                }else {
                    if (Integer.valueOf(et_number.getText().toString()) < 999) {
                        et_number.setText((Integer.valueOf(et_number.getText().toString()) + 1) + "");
                        et_number.setSelection(et_number.length());
                    }
                }
                zPrice = Integer.valueOf(et_number.getText().toString())*price;
                tv_zongjia.setText(zPrice+"元");

            }
        });

        et_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (et_number.getText() != null && !"".equals(et_number.getText().toString()) && Integer.valueOf(et_number.getText().toString()) > 1) {
                    iv_jian.setImageResource(R.drawable.sq_sl_minus);
                    iv_jian.setEnabled(true);
                    zPrice = Integer.valueOf(et_number.getText().toString())*price;
                } else {
                    iv_jian.setImageResource(R.drawable.sq_sl_minus2);
                    iv_jian.setEnabled(false);
                    zPrice = 0;
                }
                tv_zongjia.setText(zPrice+"元");
            }
        });

    }

    private void commitOrder() {
        SharedPreferences sp = OrderActivity.this.getSharedPreferences("info", Context.MODE_PRIVATE);
        String userId = sp.getString("userId", "");
        if(!"".equals(userId)){
            Net net = new Net();
            net.url = API.GOODSORDER_URL;
            net.nameSpace = API.GOODSORDER_NAMESPACE;
            net.methodName = API.GOODSORDERSAVE;
            Map<String, Object> map = new HashMap<>();
            map.put("totalNum", et_number.getText().toString());
            map.put("total", zPrice+"");
            map.put("merchantsGoodsId", getIntent().getStringExtra("goodsId"));
            map.put("userId", userId);
            net.parms = map;
            getServer(net,this);
        }
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if (flag){
            try {
                JSONObject object = new JSONObject(res);
                if(object.getBoolean("result")){
                    Intent intent = new Intent(getApplicationContext(), ZhiFuActivity.class);
                    intent.putExtra("orderId",object.getString("orderId"));
                    intent.putExtra("goodsId",getIntent().getStringExtra("goodsId"));
                    intent.putExtra("price",price+"");
                    intent.putExtra("zPrice",zPrice+"");
                    intent.putExtra("number",et_number.getText().toString());
                    intent.putExtra("goodsName",tv_goodsName.getText().toString());
                    startActivity(intent);
                    OrderActivity.this.finish();
                }
                Toast.makeText(getApplicationContext(),object.getString("msg"),Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
